package edu.whu.demo.controller;

import edu.whu.demo.entity.Commodity;
import edu.whu.demo.service.CommodityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "管理商品")
@RestController
@RequestMapping("commodity")
public class CommodityController {
    @Autowired
    CommodityService commodityService;

    // get: localhost:8088/todos/1
    @ApiOperation("根据Id查询商品信息")
    @GetMapping("/{id}")
    public ResponseEntity<Commodity> getCommodity(@ApiParam("待办事项Id")@PathVariable String id){
        Commodity result = commodityService.getCommodity(id);
        if(result==null) {
            return ResponseEntity.noContent().build();
        }else{
            return ResponseEntity.ok(result);
        }
    }

    @ApiOperation("添加商品")
    @PostMapping("")
    public ResponseEntity<Commodity> addCommodity(@RequestBody Commodity commodity){
        Commodity result = commodityService.addCommodity(commodity);
        return ResponseEntity.ok(result);
    }

    @ApiOperation("修改商品")
    @PutMapping("/{id}")
    public ResponseEntity<Void> updateCommodity(@PathVariable String id,@RequestBody Commodity commodity){
        commodityService.updateCommodity(id,commodity);
        return ResponseEntity.ok().build();
    }

    @ApiOperation("删除商品")
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteCommodity(@PathVariable String id){
        commodityService.deleteCommodity(id);
        return ResponseEntity.ok().build();
    }

}
